package logic.pruebas;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JFrame;

import logic.blocks.Block;
import logic.characters.Buu;
import logic.characters.Crash;
import logic.characters.Donatello;
import logic.characters.Falcon;
import logic.characters.Grievous;
import logic.characters.Jigglypuff;
import logic.characters.Kirby;
import logic.characters.Link;
import logic.characters.Marco;
import logic.characters.Mario;
import logic.characters.Player;
import logic.characters.Samus;
import logic.characters.Snake;
import logic.characters.Urahara;
import logic.characters.Wario;
import logic.characters.Zabuza;
import logic.enemies.Enemy;
import logic.entities.Background;
import logic.utils.MapLoader;

/** Ventana principal del juego de prueba
 * @author Unai
 */
public class GameWindow extends JFrame {
	private static final long serialVersionUID = 4393904412863366001L;
	private static Dimension TAM_MINIMO_VENTANA = new Dimension( 800, 600 );
	private GamePanel panelJuego;
	private Player jugador;

	/** Construye una ventana
	 */
	public GameWindow() { 
		setMinimumSize( TAM_MINIMO_VENTANA );
		setResizable(false);	
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		addKeyListener(new TAdapter());
		setFocusable(true);
		
		
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
		GamePanel panel = new GamePanel(300);
		panel.setLayout(null);
		add(panel);
		panel.setVisible(true);
		String character = "Mario";
		int[] abilities = new int [2];
		abilities[0] = 1;
		abilities[1] = 2;
		Mario player = new Mario(150, 350, panel, "resources" + character + "/IR.gif", 11, 1, 1, 1, 1, 1, 1, 1, abilities);
		Background background = new Background(0, 0, panel, "resources/back1.gif", player);
		setJugador(player);
		ArrayList<Block> blocks = new ArrayList<Block>();
		ArrayList<Enemy> enemies = new ArrayList<Enemy>();
		panel.setPlayer(player); 
		
		new MapLoader(blocks, enemies, panel, player, 1, 1, null);

		background.dibujar();
		
		for (Block b : blocks) {
			b.dibujar();
		}
		player.dibujar();
		panel.updateUI();
		player.enemies = enemies;
		player.blocks = blocks;		
		panel.timer.start();
		while (true) {
			player.update();
			
			for (Block b : blocks) {
				b.update(background);
				b.impact(player);
				background.updateBackground(enemies);
				for (Enemy e : enemies) {
					e.update(player);
						b.impact(e);
						e.impact(player);
				}
			}
			panel.drawTime();
			panel.updateBars();
			panel.updateUI();
		}

	}

	/** Devuelve el panel de juego de la ventana
	 * @return	Panel principal donde se visualiza el juego
	 */
	public GamePanel getPanelJuego() {
		return panelJuego;
	}

	/** Inicializa el panel de juego
	 * @param panel
	 */
	public void setPanelJuego(GamePanel panel) {
		panelJuego = panel;
	}

	/** Inicializa el jugador
	 * @param jugador
	 */
	public void setJugador (Player jugador){
		this.jugador=jugador;
	}

	/**
	 * @author Unai
	 * Clase para el reconocimiento del pulsado de teclas
	 */
	private class TAdapter extends KeyAdapter{
		public void keyReleased(KeyEvent e){
			if(jugador!=null) {
				jugador.keyReleased(e);
			}
		}
		public void keyPressed(KeyEvent e){
			if(jugador!=null) {
				jugador.keyPressed(e);
			}
		}
		
		
	}

	public static void main (String [] args) {
		
		GameWindow win = new GameWindow();
		win.pack();
		win.setLocationRelativeTo(null);
		win.setVisible(true);
		GamePanel panel = new GamePanel(300);
		panel.setLayout(null);
		win.add(panel);
		panel.setVisible(true);
		String character = "Mario";
		int[] abilities = new int [2];
		abilities[0] = 1;
		abilities[1] = 2;
		Mario player = new Mario(150, 350, panel, "resources" + character + "/IR.gif", 11, 1, 1, 1, 1, 1, 1, 1, abilities);
		Background background = new Background(0, 0, panel, "resources/back1.gif", player);
		win.setJugador(player);
		ArrayList<Block> blocks = new ArrayList<Block>();
		ArrayList<Enemy> enemies = new ArrayList<Enemy>();
		panel.setPlayer(player); 
		
		new MapLoader(blocks, enemies, panel, player, 1, 1, null);

		background.dibujar();
		
		for (Block b : blocks) {
			b.dibujar();
		}
		player.dibujar();
		panel.updateUI();
		player.enemies = enemies;
		player.blocks = blocks;		
		panel.timer.start();
		while (true) {
			player.update();
			
			for (Block b : blocks) {
				b.update(background);
				b.impact(player);
				background.updateBackground(enemies);
				for (Enemy e : enemies) {
					e.update(player);
						b.impact(e);
						e.impact(player);
				}
			}
			panel.drawTime();
			panel.updateBars();
			panel.updateUI();
		}

	}
}



